package service.impl;

import DAO.CitationDAO;
import entity.db.CitationTextDB;
import entity.db.ReferencestableDB;
import entity.text.PaperEntity;
import operation.AbstractFileOperation;
import operation.PdfAbstractFileOperation;
import operation.TxtAbstractFileOperation;
import operation.XmlAbstractFileOperation;
import service.CitationService;
import util.CollectionUtils;

import java.io.*;
import java.util.*;

/**
 * @author ：hzs
 * @date ：Created in 2020/12/4 14:50
 * @description：
 * @modified By：
 * @version: $
 */
public class CitationServiceImpl implements CitationService {

    private CitationDAO citationDAO = new CitationDAO();

    private AbstractFileOperation abstractFileOperation;

    @Override
    public List<List<Object>> getArgumentChainCitationAdjacency() {
        return citationDAO.getArgumentChainCitationAdjacency();
    }

    @Override
    public List<ReferencestableDB> getCitationAdjacencyBycommunityid(int communityid) {
        return citationDAO.getCitationAdjacencyBycommunityid(communityid);
    }

    @Override
    public Map<String, String> getFileNameByIDs(List<String> ids) {
        return citationDAO.getFileNameByIDs(ids);
    }

    @Override
    public PaperEntity readPaperFromFile(File file) {
        String name = file.getName();
        if (name.contains(".xml")) {
            abstractFileOperation = new XmlAbstractFileOperation();
        } else if (name.contains(".txt")) {
            abstractFileOperation = new TxtAbstractFileOperation();
        } else if (name.contains(".pdf")) {
            abstractFileOperation = new PdfAbstractFileOperation();
        }
        return readPaperFromFile(file, abstractFileOperation);
    }

    public PaperEntity readPaperFromFile(File file, AbstractFileOperation abstractFileOperation) {
        try {
            PaperEntity paperEntityFromFile = abstractFileOperation.getPaperEntityFromFile(file);
            return paperEntityFromFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Deprecated
    public PaperEntity readPaperFromXmlFile(File file) {
//        System.out.println("开始读xml文件");
        abstractFileOperation = new XmlAbstractFileOperation();
        try {
            PaperEntity paperEntityFromFile = abstractFileOperation.getPaperEntityFromFile(file);
            return paperEntityFromFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Deprecated
    public PaperEntity readPaperFromTxtFile(File file) {
//        System.out.println("开始读txt文件");
        abstractFileOperation = new TxtAbstractFileOperation();
        try {
            PaperEntity paperEntityFromFile = abstractFileOperation.getPaperEntityFromFile(file);
            return paperEntityFromFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Deprecated
    public PaperEntity readPaperFromPdfFile(File file) {
//        System.out.println("开始读pdf文件");
        return null;
    }

    @Override
    public List<String> extractorCitation(PaperEntity paperEntity, String citationIndex) {
        return abstractFileOperation.extractorCitationSentence(paperEntity, citationIndex);
    }

    @Override
    public Map<String, List<String>> getCitationText() {
        return citationDAO.getCitationText();
    }

    @Override
    public boolean insertCitationText(List<CitationTextDB> citationTextList) {
        return citationDAO.insertCitationText(citationTextList);
    }

    @Override
    @Deprecated
    public void filterNounPhrase() {
        Set<String> stopWordSet = new HashSet<>();
        String path = this.getClass().getClassLoader().getResource("noun_phrase_stopwords.txt").getPath();
        try(BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(path)))) {
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                stopWordSet.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<String> ids = new ArrayList<>();
        List<CitationTextDB> allCitationText = citationDAO.getAllCitationText();
        for (CitationTextDB citationTextDB : allCitationText) {
            String nounPhrase = null;
//            String nounPhrase = citationTextDB.getNounPhrase();
            if (stopWordSet.contains(nounPhrase)) {
                ids.add(citationTextDB.getId().toString());
            }
        }
        citationDAO.updateFieldDeletedById(ids);
    }

    /**
     * 看看哪些文章抽不出实体
     */
    public void test1() {
        final String format = "G:\\AITXT\\1-s2.0-%s-main.txt";
        final String filePath = "H:\\222.txt";
        BufferedReader br = null;
        List<String> ids = new ArrayList<>();
        try {
            br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(filePath)));
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                ids.add(line);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, String> fileNameByIDs = new CitationDAO().getFileNameByIDs(ids);
        for (String value : fileNameByIDs.values()) {
            File file = new File(String.format(format, value));
            if (!file.exists()) {
                System.out.println(value + " 文件不存在！");
                continue;
            }
            PaperEntity paperEntity = new CitationServiceImpl().readPaperFromFile(file);
            if (paperEntity == null
                    || CollectionUtils.isEmpty(paperEntity.getChapterEntities())) {
                System.out.println(value);
            }
        }
    }

    /**
     * 看看文章抽出来的实体是什么
     */
    public void test2() {
        File file = new File("G:\\AITXT\\1-s2.0-S000437020600110X-main.txt");
        PaperEntity paperEntity = new CitationServiceImpl().readPaperFromFile(file);
        System.out.println(paperEntity);
    }

    public static void main(String[] args) {
        new CitationServiceImpl().filterNounPhrase();
    }
}
